package cn.yoaket.unit;

import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Polygon;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Edge {

    List<Point> points;
    double width;
    double ratio;
    public Edge() {
        this.points = new ArrayList<>();
        width = 0;
        this.ratio = 0;
    }

    public double getRatio() {
        return ratio;
    }

    public void setRatio(double ratio) {
        if(ratio >= 0 && ratio <=1)
            this.ratio = ratio;
        else if(ratio > 1 || Double.isNaN(ratio) || Double.isInfinite(ratio))
            this.ratio = 1;
        else
            this.ratio = 0;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }


    public void add(Point p){
        this.points.add(p);
    }

    public void add(double x, double y){
        this.points.add(new Point(x,y));
    }

    public void add(Collection<Point> points){
        this.points.addAll(points);
    }
    public Point begin(){
        return this.points.get(0);
    }

    public double length(){
        double length = 0;
        for(int i = 0;i<size()-1;++i){
            length += Math.sqrt(Math.pow(get(i).x-get(i+1).x,2)+Math.pow(get(i).y-get(i+1).y,2));
        }
        return length;
    }

    public Point end(){
        return this.points.get(size()-1);
    }

    public Point get(int index){
        return points.get(index);
    }

    public int size(){
        return points.size();
    }

    public List<Point> vertexes(){
        return this.points;
    }

    public boolean contains(Point p){
        return this.points.contains(p);
    }

    public Polygon toPolygon(){

        final double kMax = 100;

        int length = points.size()*2;
        Coordinate[] polygon = new Coordinate[length+1];
        GeometryFactory factory = JTSFactoryFinder.getGeometryFactory();


        for(int i = 0;i< points.size();++i) {

            double k;
            if (i != points.size() - 1)
                k = (points.get(i + 1).getY() - points.get(i).getY()) / (points.get(i + 1).getX() - points.get(i).getX());
            else
                k = (points.get(i - 1).getY() - points.get(i).getY()) / (points.get(i - 1).getX() - points.get(i).getX());

            //斜率无穷大时设置一个值
            if (k > 0)
                k = Math.min(k, kMax);
            else if (k < 0)
                k = Math.max(k, -kMax);
            else if(Double.isNaN(k))
                k = kMax;


            double v_v = width / 2.0 * 1 / Math.sqrt(1 + 1 / (k * k));
            double x1 = points.get(i).getX() + v_v;
            double x2 = points.get(i).getX() - v_v;

            v_v = width / (2 * Math.sqrt(k * k + 1));
            double y1 = points.get(i).getY() - v_v;
            double y2 = points.get(i).getY() + v_v;

            polygon[i] = new Coordinate(x1, y1);
            polygon[length - i - 1] = new Coordinate(x2, y2);
        }
        polygon[length] = new Coordinate(polygon[0]);

        return factory.createPolygon(polygon);

    }


}
